cmake_minimum_required(VERSION 3.14)

project(TestQml VERSION 0.1 LANGUAGES CXX)

set(CMAKE_INCLUDE_CURRENT_DIR ON)

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# 明确指定使用Qt5版本以确保与QtLocationPlugin兼容
find_package(Qt5 REQUIRED COMPONENTS Core Quick Multimedia Sql Network Location Positioning QuickControls2)

# 添加Qt Location插件子目录（使用根目录下的QtLocationPlugin）
add_subdirectory(QtLocationPlugin)

# 主项目源文件
set(PROJECT_SOURCES
        main.cpp
        auth/DatabaseManager.cpp
        auth/DatabaseManager.h
        auth/AuthManager.cpp
        auth/AuthManager.h
        network/TcpServer.cpp
        network/TcpServer.h
        network/RobotClient.cpp
        network/RobotClient.h
        network/RobotManager.cpp
        network/RobotManager.h
        network/RobotMemoryCache.cpp
        network/RobotMemoryCache.h
        network/RouteService.cpp
        network/RouteService.h
        network/DisconnectLogger.cpp
        network/DisconnectLogger.h
        network/QMLControlInterface.cpp
        network/QMLControlInterface.h
        QtLocationPlugin/QGCFileDownload.cc
        QtLocationPlugin/QGCFileDownload.h
        QtLocationPlugin/QmlObjectListModel.cc
        QtLocationPlugin/QmlObjectListModel.h
        qml.qrc
)

# 由于明确使用Qt5，直接创建可执行文件
if(ANDROID)
    add_library(TestQml SHARED
        ${PROJECT_SOURCES}
    )
else()
    add_executable(TestQml
        ${PROJECT_SOURCES}
    )
endif()

target_compile_definitions(TestQml
  PRIVATE $<$<OR:$<CONFIG:Debug>,$<CONFIG:RelWithDebInfo>>:QT_QML_DEBUG>)

# 链接Qt5模块和自定义地图插件
target_link_libraries(TestQml
  PRIVATE 
    Qt5::Core 
    Qt5::Quick 
    Qt5::Multimedia 
    Qt5::Sql 
    Qt5::Network 
    Qt5::Location 
    Qt5::Positioning 
    Qt5::QuickControls2
    QtLocationPlugin)

# 添加Qt Location和Positioning私有头文件路径（QtLocationPlugin需要）
target_include_directories(TestQml PRIVATE 
    ${Qt5Location_PRIVATE_INCLUDE_DIRS}
    ${Qt5Positioning_PRIVATE_INCLUDE_DIRS}
)

# 添加Qt5模块的包含目录
target_include_directories(TestQml SYSTEM PRIVATE
    ${Qt5Core_INCLUDE_DIRS}
    ${Qt5Gui_INCLUDE_DIRS}
    ${Qt5Quick_INCLUDE_DIRS}
    ${Qt5Qml_INCLUDE_DIRS}
    ${Qt5Network_INCLUDE_DIRS}
    ${Qt5Sql_INCLUDE_DIRS}
    ${Qt5Location_INCLUDE_DIRS}
    ${Qt5Positioning_INCLUDE_DIRS}
)

# 添加yclocation-master目录到包含路径
target_include_directories(TestQml PRIVATE
    ${CMAKE_CURRENT_SOURCE_DIR}/yclocation-master
    ${CMAKE_CURRENT_SOURCE_DIR}/QtLocationPlugin
    ${CMAKE_CURRENT_SOURCE_DIR}/QtLocationPlugin/QMLControl
)

# 设置插件路径，确保QML可以找到QtLocationPlugin
set(QML_IMPORT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/QtLocationPlugin CACHE STRING "QML plugin search path" FORCE)
set(QML2_IMPORT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/QtLocationPlugin CACHE STRING "QML2 plugin search path" FORCE)

set_target_properties(TestQml PROPERTIES
    MACOSX_BUNDLE_GUI_IDENTIFIER my.example.com
    MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
    MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
)

# ==================== Qt QML模块部署配置 ====================

# 获取Qt安装路径
get_target_property(QMAKE_EXECUTABLE Qt5::qmake IMPORTED_LOCATION)
get_filename_component(QT_BIN_DIR "${QMAKE_EXECUTABLE}" DIRECTORY)
get_filename_component(QT_PREFIX_DIR "${QT_BIN_DIR}/.." ABSOLUTE)

# 设置Qt QML模块路径
set(QT_QML_DIR "${QT_PREFIX_DIR}/qml")

# 定义需要复制的QML模块
set(QML_MODULES_TO_COPY
    QtLocation
    QtPositioning
    QtMultimedia
    QtQuick.2
    QtQuick/Controls.2
    QtQuick/Templates.2
    QtGraphicalEffects
)

# 复制Qt QML模块到构建目录
if(WIN32)
    foreach(MODULE ${QML_MODULES_TO_COPY})
        set(MODULE_SOURCE "${QT_QML_DIR}/${MODULE}")
        set(MODULE_DEST "${CMAKE_BINARY_DIR}/${MODULE}")
        
        if(EXISTS "${MODULE_SOURCE}")
            message(STATUS "配置复制QML模块: ${MODULE}")
            add_custom_command(TARGET TestQml POST_BUILD
                COMMAND ${CMAKE_COMMAND} -E copy_directory
                    "${MODULE_SOURCE}"
                    "${MODULE_DEST}"
                COMMENT "复制Qt QML模块: ${MODULE}"
            )
        else()
            message(WARNING "QML模块不存在: ${MODULE_SOURCE}")
        endif()
    endforeach()
    
    # 额外复制Qt5的插件目录
    set(QT_PLUGINS_DIR "${QT_PREFIX_DIR}/plugins")
    set(PLUGIN_TYPES platforms imageformats iconengines mediaservice audio position geoservices)
    
    foreach(PLUGIN_TYPE ${PLUGIN_TYPES})
        set(PLUGIN_SOURCE "${QT_PLUGINS_DIR}/${PLUGIN_TYPE}")
        set(PLUGIN_DEST "${CMAKE_BINARY_DIR}/${PLUGIN_TYPE}")
        
        if(EXISTS "${PLUGIN_SOURCE}")
            message(STATUS "配置复制Qt插件: ${PLUGIN_TYPE}")
            add_custom_command(TARGET TestQml POST_BUILD
                COMMAND ${CMAKE_COMMAND} -E copy_directory
                    "${PLUGIN_SOURCE}"
                    "${PLUGIN_DEST}"
                COMMENT "复制Qt插件: ${PLUGIN_TYPE}"
            )
        endif()
    endforeach()
endif()

# Qt6特定配置（当前项目使用Qt5）
# if(QT_VERSION_MAJOR EQUAL 6)
#     qt_import_qml_plugins(TestQml)
#     qt_finalize_executable(TestQml)
# endif()